import { Sdk_Platform, WX_AD_CONFIG } from '../../frame/constants';
import util from '../tool/util'

cc.Class({
    extends: cc.Component,

    properties: {
        leftAdNode: cc.Node,
        rightAdNode: cc.Node,
        skillBtn: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    initPage() {
    },

    updatePage(param) {
        this.adSwitch(true)
        gg.data.pause = true
        this.skillSpt = this.skillBtn.getComponent('skillButton')
        if (param.skillInfo) {
            this.skillSpt.initInfo(param.skillInfo)
        } else {
            this.uiBase.closePage()
        }
    },

    closeUI() {
        for (let j = (this.leftAdNode.children.length); j > 0; j--) {
            this.leftAdNode.children[j - 1].getComponent('scrollAd').clearItem()
            he.pool.putElement('scrollAd', this.leftAdNode.children[j - 1])
        }

        for (let k = (this.rightAdNode.children.length); k > 0; k--) {
            this.rightAdNode.children[k - 1].getComponent('scrollAd').clearItem()
            he.pool.putElement('scrollAd', this.rightAdNode.children[k - 1])
        }
    },

    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    if (!common.showAd) return
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[4], this.creatAdIcon.bind(this))
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[3], this.bottomSet.bind(this))
                    break
            }
        } else {
            //  关掉广告

        }
    },

    creatAdIcon(config) {
        if (config.isOpen) {
            // for (let i = 0; i < 4; i++) {
            //     let pre = he.pool.getElement('adIcon')
            //     pre.position = cc.Vec2.ZERO
            //     pre.parent = this.leftAdNode
            //     pre.opacity = 255
            //     let spt = pre.getComponent('adIcon')
            //     spt.init(config.creatives[i], true, true, 1)
            // }
            // for (let j = 0; j < 4; j++) {
            //     let pre = he.pool.getElement('adIcon')
            //     pre.position = cc.Vec2.ZERO
            //     pre.parent = this.rightAdNode
            //     pre.opacity = 255
            //     let spt = pre.getComponent('adIcon')
            //     spt.init(config.creatives[j + 4], true, true, 1)
            // }

            let pre = he.pool.getElement('scrollAd')
            pre.parent = this.leftAdNode
            pre.getComponent('scrollAd').init(config.creatives)
        }
    },

    bottomSet(config) {
        if (config.isOpen) {
            let pre = he.pool.getElement('scrollAd')
            pre.parent = this.rightAdNode
            pre.getComponent('scrollAd').init(config.creatives)
        }
    },
    //使用技能
    userSkill() {
        this.skillSpt.changeRole()
        this.adAdd()
        this.goOnServer()
    },

    goOnServer() {
        this.uiBase.closePage()
        gg.data.pause = false
    },

    //使用花生
    userDia() {
        let priStr = `20002_${100}`
        let canBuy = gg.data.checkItemStr(priStr, false)
        if (canBuy) {
            gg.data.checkItemStr(priStr, true)
            this.userSkill()
        } else {
            gg.uiManager.addTip('花生不足')
        }
    },


    //看广告使用技能
    lookAdServer() {
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                util.adFakeTip()
                he.ad.showVideo(this.userSkill.bind(this))
                break
            case Sdk_Platform.VIVO:
                he.ad.showVideo(this.userSkill.bind(this))
                break
            case Sdk_Platform.UC:
                he.ad.showVideo(this.userSkill.bind(this))
                break
            case Sdk_Platform.OPPO:
                break
            case Sdk_Platform.HUAWEI:
                he.ad.showVideo(this.userSkill.bind(this))
                break
        }
    },
    adAdd() {
        let taskGrp = he.local.getJson('task', 'user')
        if (taskGrp && taskGrp.length > 0) {
            for (let j = 0; j < taskGrp.length; j++) {
                if (taskGrp[j].type == 9001) {
                    if (taskGrp[j].num < taskGrp[j].need) {
                        taskGrp[j].num += 1
                    }
                    break
                }
            }
        }
        he.local.setJson('task', taskGrp, 'user')
    },
});

